package org.hao.samples.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hao.samples.model.ExamScore;
import org.hao.samples.model.OneSubjectVO;
import org.hao.samples.model.Student;
import org.hao.samples.model.StudentVO;
import org.hao.samples.model.Subject;
import org.hao.samples.service.ExamTestService;
import org.hao.samples.service.StudentService;
import org.hao.samples.service.subjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentScoreController {
	@Autowired
	private subjectService subService;

	@Autowired
	private StudentService studentService;

	@Autowired
	private ExamTestService examTestService;

	@RequestMapping("/studentScores")
	public String studentScores(Model model) {
		List<Subject> subList = subService.findAllSubject();
		List<StudentVO> stuList = studentService.findAllStudentVO();

		model.addAttribute("subjectList", subList);
		model.addAttribute("studentList", stuList);
		return "studentScore/studentScore";
	}

	/**
	 * 选当前科目的所有学生
	 * 
	 * @param request
	 * @param scoreId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/scoreGetStudentBySubject")
	public Map<String, Object> scoreGetStudentBySubject(Integer sub_id) {
		HashMap<String, Object> resulMap = new HashMap<String, Object>();

		// 传学生的id和name
		List<Student> student = subService.getAllUserBySubject(sub_id);
		List<Integer> idList = new ArrayList<Integer>();
		List<String> nameList = new ArrayList<String>();
		for (int i = 0; i < student.size(); i++) {
			idList.add(student.get(i).getUser_id());
			nameList.add(student.get(i).getUser_name());
		}

		resulMap.put("idList", idList);
		resulMap.put("nameList", nameList);
		return resulMap;
	}

	/**
	 * 选当前学生的所有科目
	 * 
	 * @param request
	 * @param scoreId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/scoreGetSubjectByStudent")
	public Map<String, Object> scoreGetSubjectByStudent(Integer userId) {
		HashMap<String, Object> resulMap = new HashMap<String, Object>();

		// 传科目的id和name
		List<OneSubjectVO> subject = subService.findAllSubjectByStudent(userId);
		List<Integer> idList = new ArrayList<Integer>();
		List<String> nameList = new ArrayList<String>();
		for (int i = 0; i < subject.size(); i++) {
			idList.add(subject.get(i).getSub_id());
			nameList.add(subject.get(i).getName());
		}

		resulMap.put("idList", idList);
		resulMap.put("nameList", nameList);
		return resulMap;
	}

	/**
	 * 老师查看成绩
	 * 
	 * @param request
	 * @param scoreId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/teacherChexkScore", method = RequestMethod.POST)
	public HashMap<String, Object> teacherChexkScore(Integer subId, Integer stuId) {
		HashMap<String, Object> resulMap = new HashMap<String, Object>();
		// 当时用错，只有一个返回结果，不需要用list
		List<ExamScore> score = examTestService.teacherChexkScore(subId, stuId);
		if (score.isEmpty()) {
			resulMap.put("message", "没有参加考试");
			return resulMap;
		} else {
			resulMap.put("message", score.get(0).getScore());
			return resulMap;
		}
	}
}
